home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / numericsort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-08  |  2.5 KB  |  89 lines

  1. /*
  2.  * "$Id: numericsort.c,v 1.10 1999/03/08 14:53:44 mike Exp $"
  3.  *
  4.  * Numeric sorting routine for the Fast Light Tool Kit (FLTK).
  5.  *
  6.  * Copyright 1998-1999 by Bill Spitzak and others.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21.  * USA.
  22.  *
  23.  * Please report all bugs and problems to "fltk-bugs@easysw.com".
  24.  */
  25.  
  26. /* My own scandir sorting function, useful for the film industry where
  27.    we have many files with numbers in their names: */
  28.  
  29. #include <config.h>
  30. #include <ctype.h>
  31. #include <stdlib.h>
  32. #include <sys/types.h>
  33.  
  34. #ifdef WIN32
  35. #include <FL/filename.H>
  36. #else
  37. #if HAVE_DIRENT_H
  38. # include <dirent.h>
  39. #else
  40. # define dirent direct
  41. # if HAVE_SYS_NDIR_H
  42. #  include <sys/ndir.h>
  43. # endif
  44. # if HAVE_SYS_DIR_H
  45. #  include <sys/dir.h>
  46. # endif
  47. # if HAVE_NDIR_H
  48. #  include <ndir.h>
  49. # endif
  50. #endif
  51. #endif
  52.  
  53. #ifdef __cplusplus
  54. extern "C"
  55. #endif
  56. int numericsort(struct dirent **A, struct dirent **B) {
  57.   const char* a = (*A)->d_name;
  58.   const char* b = (*B)->d_name;
  59.   int ret = 0;
  60.   for (;;) {
  61.     if (isdigit((unsigned)*a) && isdigit((unsigned)*b)) {
  62.       int diff,magdiff;
  63.       while (*a == '0') a++;
  64.       while (*b == '0') b++;
  65.       while (isdigit((unsigned)*a) && *a == *b) {a++; b++;}
  66.       diff = (isdigit((unsigned)*a) && isdigit((unsigned)*b)) ? *a - *b : 0;
  67.       magdiff = 0;
  68.       while (isdigit((unsigned)*a)) {magdiff++; a++;}
  69.       while (isdigit((unsigned)*b)) {magdiff--; b++;}
  70.       if (magdiff) {ret = magdiff; break;} /* compare # of significant digits*/
  71.       if (diff) {ret = diff; break;}    /* compare first non-zero digit */
  72.     } else {
  73. #if 1
  74.       if ((ret = tolower((unsigned)*a)-tolower((unsigned)*b))) break; /* compare case-insensitve */
  75. #else
  76.       if ((ret = *a-*b)) break;    /* compare case-sensitive */
  77. #endif
  78.       if (!*a) break;
  79.       a++; b++;
  80.     }
  81.   }
  82.   if (!ret) return 0;
  83.   else return (ret < 0) ? -1 : 1;
  84. }
  85.  
  86. /*
  87.  * End of "$Id: numericsort.c,v 1.10 1999/03/08 14:53:44 mike Exp $".
  88.  */
  89.